<template>
    <div>
        <!-- 面包屑 -->
        <el-card>
            <el-breadcrumb separator-class="el-icon-arrow-right">
                <el-breadcrumb-item :to="{ path: '/welcom' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>商品管理</el-breadcrumb-item>
                <el-breadcrumb-item>分类管理</el-breadcrumb-item>
            </el-breadcrumb>
        </el-card>


        <!--，内容区域 -->
        <el-card>
            <!-- 添加区域 -->
            <el-row :gutter="20">
                <el-col :span="4">
                    <el-button type="primary" @click="addClassShowDialog">添加分类</el-button>
                </el-col>
            </el-row>
            <!-- 数据区域 -->
            <tree-table :data="classList" :columns="columns" :selection-type="false" :expand-type="false"
                :show-index="true" index-text="序号" border class="tree-table">
                <!-- 是否有效 -->
                <template slot="isOk">
                    <i class="el-icon-success" style="color: green;"></i>
                </template>
                <!-- 排序 -->
                <template slot="level" slot-scope="scope">
                    <el-tag v-if="scope.row.level === 1" size="mini">一级</el-tag>
                    <el-tag type="success" v-if="scope.row.level === 2" size="mini">二级</el-tag>
                    <el-tag type="warning" v-if="scope.row.level === 3" size="mini">三级</el-tag>
                </template>
                <!-- 操作 -->
                <template slot="action" slot-scope="scope">
                    <el-button type="primary" icon="el-icon-edit" size="mini"
                        @click="editClass(scope.row)">编辑</el-button>
                    <el-button type="danger" icon="el-icon-delete" size="mini" @click="delClass(scope.row)">删除</el-button>
                </template>
            </tree-table>

        </el-card>
        <!-- 添加弹框 -->
        <el-dialog title="添加分类" :visible.sync="addClassDialogVisible" width="30%" @close="addDialogClosed">
            <!-- 内容 -->
            <el-form :model="addClassForm" :rules="addClassFormRules" ref="addClassFormRef" label-width="100px">
                <el-form-item label="分类名称" prop="name">
                    <el-input v-model="addClassForm.name"></el-input>
                </el-form-item>
                <!-- change-on-select允许选择任意一级的选项
                clearable支持清空选项 -->
                <el-form-item label="父级分类">
                    <el-cascader change-on-select clearable expand-trigger="hover" :options="parentCateList"
                        v-model="selectedKeys" :props="cascaderProps" @change="parentCateChange">
                    </el-cascader>
                </el-form-item>

            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addClassDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addGoodsClass">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 编辑弹窗 -->
        <el-dialog title="编辑分类" :visible.sync="editClassDialogVisible" width="30%" @close="editDialogClosed">
            <!-- 内容 -->
            <el-form :model="editClassForm" :rules="addClassFormRules" ref="editClassFormRef" label-width="100px">
                <el-form-item label="分类名称" prop="name">
                    <el-input v-model="editClassForm.name"></el-input>
                </el-form-item>
                <!-- change-on-select允许选择任意一级的选项
                clearable支持清空选项 -->
                <el-form-item label="父级分类" v-if="editClassForm.level != 1">
                    <el-cascader expand-trigger="hover" :options="parentCateList" v-model="editSelectedKeys"
                        :props="cascaderProps" @change="editParentCateChange">
                    </el-cascader>
                </el-form-item>

            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editClassDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="editGoodsClass">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>



<script>
import { getGoodsClassByLevel, getClassAll, getClassTwo, insertGoodsClass, updateGoodsClass, deleteGoodsClassById } from '../../api/goodsClass/goodsClass'
export default {
    data() {
        return {
            //添加的数据承载
            addClassForm: {
                //分类名称
                name: '',
                //父级分类id,默认为空，表示一级分类
                paretId: null,
                //分类的等级,默认添加一级分类
                level: 1,
            },
            //添加弹框按钮
            addClassDialogVisible: false,
            //数据
            classList: [],
            columns: [
                {
                    label: '分类名称',
                    prop: "name"
                },
                {
                    label: "是否有效",
                    // 表示，将当前列定义为模板列
                    type: "template",
                    // 表示当前这一列使用的模板名称
                    template: "isOk"
                },
                {
                    label: "排序",
                    // 表示，将当前列定义为模板列
                    type: "template",
                    // 表示当前这一列使用的模板名称
                    template: "level"
                },
                {
                    label: "操作",
                    // 表示，将当前列定义为模板列
                    type: "template",
                    // 表示当前这一列使用的模板名称
                    template: "action"
                }
            ],
            //添加表单的验证规则
            addClassFormRules: {
                name: [
                    { required: true, message: '请输入分类名称', trigger: 'blur' },
                ]
            },
            //父级分类的列表
            parentCateList: [],
            //指定级联选择器的配置对象
            cascaderProps: {
                value: 'id',
                label: 'name',
                children: 'children'
            },
            //选中的父级分类的ID数组
            selectedKeys: [],
            //编辑弹窗的弹窗状态
            editClassDialogVisible: false,
            //编辑表单的数据承载
            editClassForm: {
                name: '',
                paretId: null,
                level: 1,
                id: null
            },
            editSelectedKeys: []
        }
    },
    created() {
        this.getData()
    },
    methods: {
        //编辑确定按钮
        async editGoodsClass() {
            await updateGoodsClass(this.editClassForm).then(res => {
                if (res.data.status == 0) {
                    this.getData()
                    this.editClassDialogVisible = false
                    this.$message({
                        message: '修改成功',
                        type: 'success'
                    })
                } else {
                    this.$message.error('修改失败')
                }
            })

        },
        //编辑弹框级联选择器发生改变时
        editParentCateChange() {
            this.editClassForm.paretId = this.editSelectedKeys[this.editSelectedKeys.length - 1]
        },

        //根据level获取分类数据
        async getClassByLevel(level) {
            await getGoodsClassByLevel(level).then(res => {
                console.log(res)
                if (res.data.status == 0) {
                    this.parentCateList = res.data.data
                }
            })
        },
        //编辑弹窗关闭时，重置
        editDialogClosed() {
            this.$refs.editClassFormRef.resetFields()
            this.editSelectedKeys = []
            
        },
        //编辑
        async editClass(row) {
            this.editClassDialogVisible = true
            if (row.level === 1) {
                this.editClassForm.name = row.name
                this.editClassForm.id = row.id
                return
            }
            if (row.level === 2) {
                this.getClassByLevel(1)
                this.editSelectedKeys = [row.paretId]
            }
            if (row.level === 3) {
                this.getDataTwo()
                const res = await getGoodsClassByLevel(2)
                let id = res.data.data.find(item => item.id === row.paretId).paretId
                this.editSelectedKeys = [id, row.paretId]
            }
            this.editClassForm.id = row.id
            this.editClassForm.name = row.name
            this.editClassForm.level = row.level
            this.editClassForm.paretId = row.paretId

        },
        //删除
        delClass(row) {
            this.$confirm('确认删除吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                await deleteGoodsClassById(row.id).then(res => {
                    console.log(res)
                    if (res.data.status === 0) {
                        this.getData()
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        })
                    }
                })
            }).catch(()=>{})
        },
        //弹窗关闭时触发
        addDialogClosed() {
            //关闭校验以及重置
            this.$refs.addClassFormRef.resetFields()
            this.selectedKeys = []
        },
        //添加按钮
        addGoodsClass() {
            this.$refs.addClassFormRef.validate(async valid => {
                if (!valid) return
                await insertGoodsClass(this.addClassForm).then(res => {
                    console.log(res)
                    if (res.data.status == 0) {
                        this.getData()
                        this.addClassDialogVisible = false
                        this.$message({
                            message: '添加成功',
                            type: 'success'
                        })
                    } else {
                        this.$message.error('添加失败')
                    }
                })
            })
        },
        //父级选择器发生改变触发
        parentCateChange() {
            if (this.selectedKeys.length > 0) {
                this.addClassForm.paretId = this.selectedKeys[this.selectedKeys.length - 1]
                this.addClassForm.level = this.selectedKeys.length + 1
            } else {
                this.addClassForm.paretId = null
                this.addClassForm.level = 1
            }

        },
        //获取一级二级分类信息
        async getDataTwo() {
            await getClassTwo().then(res => {
                if (res.data.status == 0) {
                    this.parentCateList = res.data.data
                }
            })
        },
        //点击添加弹框显示
        addClassShowDialog() {
            this.addClassDialogVisible = true
            this.getDataTwo()
        },
        //获取数据
        getData() {
            getClassAll().then(res => {
                if (res.data.status == 0) {
                    this.classList = res.data.data
                }
            })
        }
    }
}
</script>

<style scoped lang="less">
.el-row {
    margin-bottom: 20px;
}

.el-cascader {
    width: 100%;
}
</style>